# encoding=utf8
import sys
reload(sys)
sys.setdefaultencoding('utf8')

class JT_0fb2:
    
    Data_array1 = [["时间","12"],["车速和发动机转速各分段数据个数","2"],["油门开度各分段数据个数","2"],["时间片段的最高时速","4"],["时间片段的最高转速","4"],["时间片段的油耗","4"],["时间片段的制动使用里程","4"],["时间片段的辅助制动使用里程","4"],["车速和发动机转速各分段数据列表","0"],["油门开度各分段数据列表","0"],["时间片段的混合制动使用里程","4"],["灯具使用情况汇总数据","4"],["单体开关次数汇总数据","1"],["后视镜调整开关汇总数据","1"]]
    protocolStr = ''
    data_g_str1 = '' #数据组1
    result = []

    def LogicAisle(self, protocol, header):
        self.result = header
        self.protocolStr = protocol
        self.data_g_str1 = self.protocolStr[0:]
        flag1 = self.Data_array1
        startPoint1=0
        
        speedFlag = 0
        oilFlag = 0
        
        #=speedFlag = 0
        #rearviewFlag = 0
        for i in range(len(flag1)):
            dataarrayLen = flag1[i][1]
            print type(startPoint1)
            print type(int(dataarrayLen))
            if (flag1[i][0] == "车速和发动机转速各分段数据列表" and speedFlag == 1):
                endPoint = startPoint1+dataLen*4 #4是车速和发动机转速各分段数据列表的长度
                print "车速endPoint",endPoint
            elif (flag1[i][0] == "油门开度各分段数据列表" and oilFlag == 1):
                if (speedFlag == 1):
                    endPoint = startPoint1+4+dataLen*3
                else:
                    endPoint = startPoint1+dataLen*3
                    print "11111111startPoint1",startPoint1
                    print "11111111endPoint",endPoint
            else :
                endPoint = startPoint1+int(dataarrayLen)
            print "startPoint1",startPoint1
            print "endPoint",endPoint
            tmpdata = self.data_g_str1[startPoint1:endPoint]
            try:
                dataLen=int(tmpdata,16)
            except ValueError:
                print "ValueError: invalid literal for int() with base 16: ''"
            else:
                print "pass"
            print "tmpdata",tmpdata
            print "dataLen",dataLen
            if (flag1[i][0] == "车速和发动机转速各分段数据个数" and dataLen > 0 ):
                speedFlag = 1
                print "speedFlag",speedFlag
            if (flag1[i][0] == "油门开度各分段数据个数" and dataLen > 0 ):
                oilFlag = 1
                print "oilFlag",oilFlag
            
            print "tmpdata",tmpdata
            try:
                tmpdataLen = int(tmpdata,16)
            except ValueError:
                print "ValueError: invalid literal for int() with base 16: ''"
            else:
                print "pass"
            if (flag1[i][0] == "单体开关次数汇总数据" and tmpdataLen > 0):
                self.result.append("灯具数据个数" + ':' + tmpdata + '\n')
                tmpdata = self.data_g_str1[startPoint1:endPoint+5*tmpdataLen]
                startPoint1 = endPoint+5*tmpdataLen
                singleFlag = 1
            elif (flag1[i][0] == "后视镜调整开关汇总数据" and tmpdataLen > 0 ):
                self.result.append("开关项数据个数" + ':' + tmpdata + '\n')
                tmpdata = self.data_g_str1[startPoint1:endPoint+5*tmpdataLen]
                startPoint1 = endPoint+5*tmpdataLen
            else:
                startPoint1 = endPoint
            print type(flag1[i][0])
            print "flag1[i][0]",(flag1[i][0])
            self.result.append(flag1[i][0] + ':' + tmpdata + '\n')
